<?php
/**
 * 笔记模型
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/12 15:49
 */

namespace App\Model;


class Note extends Model
{
    /**
     * 获取内容
     * @return array
     * @throws \Exception
     */
    public function getNote()
    {
        $book_id = $_REQUEST['id'] ?? 0;

        if ($book_id) {
            $info = $this->where(['book_id' => $book_id, 'user_id' => $this->user_id])->find();
        }else {
            $info = $this->where(['user_id' => $this->user_id])->order('book_id', 'asc')->find();
        }
        $info = $info ? $info->toArray() : ['book_id' => 0, 'content' => '欢迎使用在线云笔记'];

        return ['code' => 200, 'msg' => 'ok', 'data' => $info];
    }

    /**
     * 添加新note book
     * @return array
     */
    public function newNote()
    {
        $data = $_POST;
        $data['type'] = 0;
        if ($data['id']>0) {
            $data['parent_id'] = $data['id'];
        }
        $data['user_id'] = $this->user_id;
        $data['created_at'] = $_SERVER['REQUEST_TIME'];
        $data['updated_at'] = $_SERVER['REQUEST_TIME'];
        unset($data['id']);

        $model = new NoteBook();
        $id    = $model->insertGetId($data);
        if ($id) {
            return ['code' => 200, 'msg' => '保存成功', 'data' => ['id' => $id]];
        }
        return ['code' => 400, 'msg' => '保存失败', 'data' => ['id' => $id]];
    }

    /**
     * 编辑
     * @return array
     */
    public function renameNote()
    {
        $data = $_POST;
        $data['updated_at'] = $_SERVER['REQUEST_TIME'];
        $model = new NoteBook();

        if ($model->save($data, ['id' => $data['id']])) {
            return ['code' => 200, 'msg' => '编辑成功', 'data' => []];
        }
        return ['code' => 400, 'msg' => '编辑失败', 'data' => []];
    }

    /**
     * 添加新note book
     * @return array
     */
    public function newNotebook()
    {
        $data = $_POST;
        $data['type'] = 1;

        $data['user_id'] = $this->user_id;
        $data['created_at'] = $_SERVER['REQUEST_TIME'];
        $data['updated_at'] = $_SERVER['REQUEST_TIME'];

        $model = new NoteBook();
        $id    = $model->insertGetId($data);
        if ($id) {
            return ['code' => 200, 'msg' => '保存成功', 'data' => ['id' => $id]];
        }
        return ['code' => 400, 'msg' => '保存失败', 'data' => ['id' => $id]];
    }

    /**
     * 删除
     * @return array
     * @throws \Exception
     */
    public function deleteNote()
    {
        $book_id = $_POST['id'];
        $model   = new NoteBook();
        if ($model->where(['id' => $book_id, 'user_id' => $this->user_id])->delete()) {
            $this->where(['book_id' => $book_id, 'user_id' => $this->user_id])->delete();
            //取最小的一条
            $id = $model->getMinId();
            return ['code' => 200, 'msg' => '保存成功', 'data' => ['id' => (int)$id]];
        }
        return ['code' => 400, 'msg' => '删除失败', 'data' => []];
    }

    /**
     * 保存内容
     * @return array
     * @throws \Exception
     */
    public function saveNote()
    {
        $data = $_POST;
        $data['user_id'] = $this->user_id;

        $where = ['book_id' => $data['book_id'], 'user_id' => $this->user_id];
        $model = $this->where($where)->find();
        if ($model) {
            $data['updated_at'] = $_SERVER['REQUEST_TIME'];
            if (!$model->save($data)) {
                return ['code' => 400, 'msg' => '保存失败', 'data' => []];
            }
            $id = $model->id;
        }else {
            $data['created_at'] = $_SERVER['REQUEST_TIME'];
            $data['updated_at'] = $_SERVER['REQUEST_TIME'];
            $id = $this->insertGetId($data);
        }

        return ['code' => 200, 'msg' => '保存成功', 'data' => ['id' => $data['book_id']]];
    }

    /**
     * 获取内容
     * @return array
     * @throws \Exception
     */
    public function getNoteSettings()
    {
        $book_id = $_POST['id'];
        //book信息
        $book = (new NoteBook())->field(['id', 'title', 'created_at', 'updated_at'])->where(['id' => $book_id, 'user_id' => $this->user_id])->find();
        if ($book) {
            $update_at = $this->where(['book_id' => $book_id])->value('updated_at');
            if ($update_at) $book['updated_at'] = $update_at;
            $book['share'] = 0;
            $book['created_at'] = date('Y-m-d H:i:s', $book['created_at']);
            $book['updated_at'] = date('Y-m-d H:i:s', $book['updated_at']);
            return ['code' => 200, 'msg' => '获取成功', 'data' => $book];
        }
        return ['code' => 200, 'msg' => '获取失败', 'data' => []];
    }

    /**
     * 目录列表
     * @throws \Exception
     */
    public function noteBookList()
    {
        $model = new NoteBook();
        $model->setUserId($this->user_id);
        //目录
        $dataList = $model->getNoteBookList();
        //用户信息
        $userModel= new User();
        $userModel->setUserId($this->user_id);
        $user     = $userModel->getInfo();
        return ['code' => 200, 'msg' => '获取成功', 'data' => $dataList, 'user' => $user];
    }
}